package com.dobby.pea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dobby.pea.entity.tbGood;
import com.dobby.pea.entity.tbGoodSales;
import com.dobby.pea.mapper.GoodSalesMapper;
import com.dobby.pea.service.GoodSalesService;
import com.dobby.pea.service.GoodService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Dobby
 * @version 1.0
 */

@Service
public class GoodSalesServiceImpl extends ServiceImpl<GoodSalesMapper, tbGoodSales> implements GoodSalesService {


    @Transactional
    public tbGoodSales GoodBySales(Long goodId) {
        LambdaQueryWrapper<tbGoodSales> queryWrappertbGoodSales = new LambdaQueryWrapper<>();
        queryWrappertbGoodSales.eq(tbGoodSales::getGoodId,goodId);
        return this.getOne(queryWrappertbGoodSales);

    }

    @Transactional
    public boolean SalesAddOne(tbGood tbgood) {
        tbGoodSales goodSales = this.GoodBySales(tbgood.getId());
        if (goodSales==null){
            return false;
        }

        goodSales.setSales(goodSales.getSales()+1);
        return this.updateById(goodSales);


    }

    @Transactional
    public void SalesDelete(tbGood tbGood) {
        tbGoodSales goodSales = this.GoodBySales(tbGood.getId());
        this.removeById(goodSales);
    }
}
